//
//  ViewController.m
//  2D绘图（文字、图片）
//
//  Created by apple on 15/9/18.
//  Copyright (c) 2015年 戴维营. All rights reserved.
//

#import "ViewController.h"
#import "Text.h"

@interface ViewController ()

{
    Text *text;
}

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    text = [[Text alloc] initWithFrame:[UIScreen mainScreen].bounds];
    
    text.backgroundColor = [UIColor whiteColor];
    
    [self.view addSubview:text];
    
//    [NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(didTimer) userInfo:nil repeats:YES];

}

- (void)didTimer
{
    [text setNeedsDisplay];
}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
#if 0
    //开始进入图像上下文
    
    UIGraphicsBeginImageContext(CGSizeMake(300, 300));
    
    //获取当前图像上下文
    
    CGContextRef context = UIGraphicsGetCurrentContext();
    
    CGContextAddEllipseInRect(context, CGRectMake(100, 100, 100, 100));
   
    [[UIColor greenColor] setFill];
    
    CGContextFillPath(context);
    
    
    [[UIColor orangeColor] setFill];
    
    CGContextAddRect(context, CGRectMake(50, 50, 50, 50));
    
//    CGContextFillPath(context);
    
    CGContextDrawPath(context, kCGPathEOFillStroke);
    
    
    //从当前图像上下文中获取图像
    
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    
    
    //使用JPG格式压缩，质量为0.0-1.0
    
    NSData *jpgData = UIImageJPEGRepresentation(image, 1.0);
    
    [jpgData writeToFile:@"/Users/apple/Desktop/test.jpg" atomically:YES];
    
    NSData *pngData = UIImagePNGRepresentation(image);
    
    [pngData writeToFile:@"/Users/apple/Desktop/texst.png" atomically:YES];
    
    //结束上下文
    
    UIGraphicsEndImageContext();
    
#endif
    
#if 1
    
    UIGraphicsBeginImageContext(CGSizeMake(10, 10));
    
#if 1
    [self.view drawViewHierarchyInRect:CGRectMake(0, 0, 100, 100) afterScreenUpdates:YES];
 
#endif
    
#if 0
    
    CGContextRef context = UIGraphicsGetCurrentContext();
    
    [self.view.layer renderInContext:context];
    
#endif
    
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    
    NSData *pngData = UIImagePNGRepresentation(image);
    
    [pngData writeToFile:@"/Users/apple/Desktop/text.png" atomically:YES];
    
    UIGraphicsEndImageContext();
    
//    CGContextRef context = UIGraphicsGetCurrentContext();
//    
//    [self.view.layer renderInContext:context];
//    

    
   #endif
    
    
    
    
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
